/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladoresJPA;

import controladoresJPA.exceptions.IllegalOrphanException;
import controladoresJPA.exceptions.NonexistentEntityException;
import controladoresJPA.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import modelo.Consumidor;
import modelo.Reporte;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;
import modelo.Categoria;
import modelo.Ingreso;
import modelo.OperarConsumoIngreso;
import modelo.TransferenciaIngreso;
import modelo.OperarIngreso;

/**
 *
 * @author Francisco
 */
public class IngresoJpaController implements Serializable {

    public IngresoJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    public void altaIngreso(Ingreso ingreso) throws RollbackFailureException, Exception{
        EntityManager em = null;
        try{
            utx.begin();
                em = getEntityManager();
                em.persist(ingreso);
            utx.commit();
        }catch(Exception e){
            try {
                utx.rollback();
            } catch (Exception re) {
                re.printStackTrace();
                throw new RollbackFailureException("Ocurrio un error.", re);
            }
            e.printStackTrace();
            throw e;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
    public void create(Ingreso ingreso) throws RollbackFailureException, Exception {
        if (ingreso.getReporteList() == null) {
            ingreso.setReporteList(new ArrayList<Reporte>());
        }
        if (ingreso.getCategoriaList() == null) {
            ingreso.setCategoriaList(new ArrayList<Categoria>());
        }
        if (ingreso.getOperarConsumoIngresoList() == null) {
            ingreso.setOperarConsumoIngresoList(new ArrayList<OperarConsumoIngreso>());
        }
        if (ingreso.getTransferenciaIngresoList() == null) {
            ingreso.setTransferenciaIngresoList(new ArrayList<TransferenciaIngreso>());
        }
        if (ingreso.getOperarIngresoList() == null) {
            ingreso.setOperarIngresoList(new ArrayList<OperarIngreso>());
        }
        if (ingreso.getOperarIngresoList1() == null) {
            ingreso.setOperarIngresoList1(new ArrayList<OperarIngreso>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Consumidor idConsumidor = ingreso.getIdConsumidor();
            if (idConsumidor != null) {
                idConsumidor = em.getReference(idConsumidor.getClass(), idConsumidor.getIdConsumidor());
                ingreso.setIdConsumidor(idConsumidor);
            }
            List<Reporte> attachedReporteList = new ArrayList<Reporte>();
            for (Reporte reporteListReporteToAttach : ingreso.getReporteList()) {
                reporteListReporteToAttach = em.getReference(reporteListReporteToAttach.getClass(), reporteListReporteToAttach.getIdReporte());
                attachedReporteList.add(reporteListReporteToAttach);
            }
            ingreso.setReporteList(attachedReporteList);
            List<Categoria> attachedCategoriaList = new ArrayList<Categoria>();
            for (Categoria categoriaListCategoriaToAttach : ingreso.getCategoriaList()) {
                categoriaListCategoriaToAttach = em.getReference(categoriaListCategoriaToAttach.getClass(), categoriaListCategoriaToAttach.getIdCategoria());
                attachedCategoriaList.add(categoriaListCategoriaToAttach);
            }
            ingreso.setCategoriaList(attachedCategoriaList);
            List<OperarConsumoIngreso> attachedOperarConsumoIngresoList = new ArrayList<OperarConsumoIngreso>();
            for (OperarConsumoIngreso operarConsumoIngresoListOperarConsumoIngresoToAttach : ingreso.getOperarConsumoIngresoList()) {
                operarConsumoIngresoListOperarConsumoIngresoToAttach = em.getReference(operarConsumoIngresoListOperarConsumoIngresoToAttach.getClass(), operarConsumoIngresoListOperarConsumoIngresoToAttach.getIdOperarConsumoIngreso());
                attachedOperarConsumoIngresoList.add(operarConsumoIngresoListOperarConsumoIngresoToAttach);
            }
            ingreso.setOperarConsumoIngresoList(attachedOperarConsumoIngresoList);
            List<TransferenciaIngreso> attachedTransferenciaIngresoList = new ArrayList<TransferenciaIngreso>();
            for (TransferenciaIngreso transferenciaIngresoListTransferenciaIngresoToAttach : ingreso.getTransferenciaIngresoList()) {
                transferenciaIngresoListTransferenciaIngresoToAttach = em.getReference(transferenciaIngresoListTransferenciaIngresoToAttach.getClass(), transferenciaIngresoListTransferenciaIngresoToAttach.getIdTransferenciaIngreso());
                attachedTransferenciaIngresoList.add(transferenciaIngresoListTransferenciaIngresoToAttach);
            }
            ingreso.setTransferenciaIngresoList(attachedTransferenciaIngresoList);
            List<OperarIngreso> attachedOperarIngresoList = new ArrayList<OperarIngreso>();
            for (OperarIngreso operarIngresoListOperarIngresoToAttach : ingreso.getOperarIngresoList()) {
                operarIngresoListOperarIngresoToAttach = em.getReference(operarIngresoListOperarIngresoToAttach.getClass(), operarIngresoListOperarIngresoToAttach.getIdOperarIngreso());
                attachedOperarIngresoList.add(operarIngresoListOperarIngresoToAttach);
            }
            ingreso.setOperarIngresoList(attachedOperarIngresoList);
            List<OperarIngreso> attachedOperarIngresoList1 = new ArrayList<OperarIngreso>();
            for (OperarIngreso operarIngresoList1OperarIngresoToAttach : ingreso.getOperarIngresoList1()) {
                operarIngresoList1OperarIngresoToAttach = em.getReference(operarIngresoList1OperarIngresoToAttach.getClass(), operarIngresoList1OperarIngresoToAttach.getIdOperarIngreso());
                attachedOperarIngresoList1.add(operarIngresoList1OperarIngresoToAttach);
            }
            ingreso.setOperarIngresoList1(attachedOperarIngresoList1);
            em.persist(ingreso);
            if (idConsumidor != null) {
                idConsumidor.getIngresoList().add(ingreso);
                idConsumidor = em.merge(idConsumidor);
            }
            for (Reporte reporteListReporte : ingreso.getReporteList()) {
                reporteListReporte.getIngresoList().add(ingreso);
                reporteListReporte = em.merge(reporteListReporte);
            }
            for (Categoria categoriaListCategoria : ingreso.getCategoriaList()) {
                categoriaListCategoria.getIngresoList().add(ingreso);
                categoriaListCategoria = em.merge(categoriaListCategoria);
            }
            for (OperarConsumoIngreso operarConsumoIngresoListOperarConsumoIngreso : ingreso.getOperarConsumoIngresoList()) {
                Ingreso oldIdIngresoOfOperarConsumoIngresoListOperarConsumoIngreso = operarConsumoIngresoListOperarConsumoIngreso.getIdIngreso();
                operarConsumoIngresoListOperarConsumoIngreso.setIdIngreso(ingreso);
                operarConsumoIngresoListOperarConsumoIngreso = em.merge(operarConsumoIngresoListOperarConsumoIngreso);
                if (oldIdIngresoOfOperarConsumoIngresoListOperarConsumoIngreso != null) {
                    oldIdIngresoOfOperarConsumoIngresoListOperarConsumoIngreso.getOperarConsumoIngresoList().remove(operarConsumoIngresoListOperarConsumoIngreso);
                    oldIdIngresoOfOperarConsumoIngresoListOperarConsumoIngreso = em.merge(oldIdIngresoOfOperarConsumoIngresoListOperarConsumoIngreso);
                }
            }
            for (TransferenciaIngreso transferenciaIngresoListTransferenciaIngreso : ingreso.getTransferenciaIngresoList()) {
                Ingreso oldIdIngresoOfTransferenciaIngresoListTransferenciaIngreso = transferenciaIngresoListTransferenciaIngreso.getIdIngreso();
                transferenciaIngresoListTransferenciaIngreso.setIdIngreso(ingreso);
                transferenciaIngresoListTransferenciaIngreso = em.merge(transferenciaIngresoListTransferenciaIngreso);
                if (oldIdIngresoOfTransferenciaIngresoListTransferenciaIngreso != null) {
                    oldIdIngresoOfTransferenciaIngresoListTransferenciaIngreso.getTransferenciaIngresoList().remove(transferenciaIngresoListTransferenciaIngreso);
                    oldIdIngresoOfTransferenciaIngresoListTransferenciaIngreso = em.merge(oldIdIngresoOfTransferenciaIngresoListTransferenciaIngreso);
                }
            }
            for (OperarIngreso operarIngresoListOperarIngreso : ingreso.getOperarIngresoList()) {
                Ingreso oldIdIngresoOrigenOfOperarIngresoListOperarIngreso = operarIngresoListOperarIngreso.getIdIngresoOrigen();
                operarIngresoListOperarIngreso.setIdIngresoOrigen(ingreso);
                operarIngresoListOperarIngreso = em.merge(operarIngresoListOperarIngreso);
                if (oldIdIngresoOrigenOfOperarIngresoListOperarIngreso != null) {
                    oldIdIngresoOrigenOfOperarIngresoListOperarIngreso.getOperarIngresoList().remove(operarIngresoListOperarIngreso);
                    oldIdIngresoOrigenOfOperarIngresoListOperarIngreso = em.merge(oldIdIngresoOrigenOfOperarIngresoListOperarIngreso);
                }
            }
            for (OperarIngreso operarIngresoList1OperarIngreso : ingreso.getOperarIngresoList1()) {
                Ingreso oldIdIngresoDestinoOfOperarIngresoList1OperarIngreso = operarIngresoList1OperarIngreso.getIdIngresoDestino();
                operarIngresoList1OperarIngreso.setIdIngresoDestino(ingreso);
                operarIngresoList1OperarIngreso = em.merge(operarIngresoList1OperarIngreso);
                if (oldIdIngresoDestinoOfOperarIngresoList1OperarIngreso != null) {
                    oldIdIngresoDestinoOfOperarIngresoList1OperarIngreso.getOperarIngresoList1().remove(operarIngresoList1OperarIngreso);
                    oldIdIngresoDestinoOfOperarIngresoList1OperarIngreso = em.merge(oldIdIngresoDestinoOfOperarIngresoList1OperarIngreso);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Ingreso ingreso) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Ingreso persistentIngreso = em.find(Ingreso.class, ingreso.getIdIngreso());
            Consumidor idConsumidorOld = persistentIngreso.getIdConsumidor();
            Consumidor idConsumidorNew = ingreso.getIdConsumidor();
            List<Reporte> reporteListOld = persistentIngreso.getReporteList();
            List<Reporte> reporteListNew = ingreso.getReporteList();
            List<Categoria> categoriaListOld = persistentIngreso.getCategoriaList();
            List<Categoria> categoriaListNew = ingreso.getCategoriaList();
            List<OperarConsumoIngreso> operarConsumoIngresoListOld = persistentIngreso.getOperarConsumoIngresoList();
            List<OperarConsumoIngreso> operarConsumoIngresoListNew = ingreso.getOperarConsumoIngresoList();
            List<TransferenciaIngreso> transferenciaIngresoListOld = persistentIngreso.getTransferenciaIngresoList();
            List<TransferenciaIngreso> transferenciaIngresoListNew = ingreso.getTransferenciaIngresoList();
            List<OperarIngreso> operarIngresoListOld = persistentIngreso.getOperarIngresoList();
            List<OperarIngreso> operarIngresoListNew = ingreso.getOperarIngresoList();
            List<OperarIngreso> operarIngresoList1Old = persistentIngreso.getOperarIngresoList1();
            List<OperarIngreso> operarIngresoList1New = ingreso.getOperarIngresoList1();
            List<String> illegalOrphanMessages = null;
            for (OperarConsumoIngreso operarConsumoIngresoListOldOperarConsumoIngreso : operarConsumoIngresoListOld) {
                if (!operarConsumoIngresoListNew.contains(operarConsumoIngresoListOldOperarConsumoIngreso)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain OperarConsumoIngreso " + operarConsumoIngresoListOldOperarConsumoIngreso + " since its idIngreso field is not nullable.");
                }
            }
            for (TransferenciaIngreso transferenciaIngresoListOldTransferenciaIngreso : transferenciaIngresoListOld) {
                if (!transferenciaIngresoListNew.contains(transferenciaIngresoListOldTransferenciaIngreso)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain TransferenciaIngreso " + transferenciaIngresoListOldTransferenciaIngreso + " since its idIngreso field is not nullable.");
                }
            }
            for (OperarIngreso operarIngresoList1OldOperarIngreso : operarIngresoList1Old) {
                if (!operarIngresoList1New.contains(operarIngresoList1OldOperarIngreso)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain OperarIngreso " + operarIngresoList1OldOperarIngreso + " since its idIngresoDestino field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (idConsumidorNew != null) {
                idConsumidorNew = em.getReference(idConsumidorNew.getClass(), idConsumidorNew.getIdConsumidor());
                ingreso.setIdConsumidor(idConsumidorNew);
            }
            List<Reporte> attachedReporteListNew = new ArrayList<Reporte>();
            for (Reporte reporteListNewReporteToAttach : reporteListNew) {
                reporteListNewReporteToAttach = em.getReference(reporteListNewReporteToAttach.getClass(), reporteListNewReporteToAttach.getIdReporte());
                attachedReporteListNew.add(reporteListNewReporteToAttach);
            }
            reporteListNew = attachedReporteListNew;
            ingreso.setReporteList(reporteListNew);
            List<Categoria> attachedCategoriaListNew = new ArrayList<Categoria>();
            for (Categoria categoriaListNewCategoriaToAttach : categoriaListNew) {
                categoriaListNewCategoriaToAttach = em.getReference(categoriaListNewCategoriaToAttach.getClass(), categoriaListNewCategoriaToAttach.getIdCategoria());
                attachedCategoriaListNew.add(categoriaListNewCategoriaToAttach);
            }
            categoriaListNew = attachedCategoriaListNew;
            ingreso.setCategoriaList(categoriaListNew);
            List<OperarConsumoIngreso> attachedOperarConsumoIngresoListNew = new ArrayList<OperarConsumoIngreso>();
            for (OperarConsumoIngreso operarConsumoIngresoListNewOperarConsumoIngresoToAttach : operarConsumoIngresoListNew) {
                operarConsumoIngresoListNewOperarConsumoIngresoToAttach = em.getReference(operarConsumoIngresoListNewOperarConsumoIngresoToAttach.getClass(), operarConsumoIngresoListNewOperarConsumoIngresoToAttach.getIdOperarConsumoIngreso());
                attachedOperarConsumoIngresoListNew.add(operarConsumoIngresoListNewOperarConsumoIngresoToAttach);
            }
            operarConsumoIngresoListNew = attachedOperarConsumoIngresoListNew;
            ingreso.setOperarConsumoIngresoList(operarConsumoIngresoListNew);
            List<TransferenciaIngreso> attachedTransferenciaIngresoListNew = new ArrayList<TransferenciaIngreso>();
            for (TransferenciaIngreso transferenciaIngresoListNewTransferenciaIngresoToAttach : transferenciaIngresoListNew) {
                transferenciaIngresoListNewTransferenciaIngresoToAttach = em.getReference(transferenciaIngresoListNewTransferenciaIngresoToAttach.getClass(), transferenciaIngresoListNewTransferenciaIngresoToAttach.getIdTransferenciaIngreso());
                attachedTransferenciaIngresoListNew.add(transferenciaIngresoListNewTransferenciaIngresoToAttach);
            }
            transferenciaIngresoListNew = attachedTransferenciaIngresoListNew;
            ingreso.setTransferenciaIngresoList(transferenciaIngresoListNew);
            List<OperarIngreso> attachedOperarIngresoListNew = new ArrayList<OperarIngreso>();
            for (OperarIngreso operarIngresoListNewOperarIngresoToAttach : operarIngresoListNew) {
                operarIngresoListNewOperarIngresoToAttach = em.getReference(operarIngresoListNewOperarIngresoToAttach.getClass(), operarIngresoListNewOperarIngresoToAttach.getIdOperarIngreso());
                attachedOperarIngresoListNew.add(operarIngresoListNewOperarIngresoToAttach);
            }
            operarIngresoListNew = attachedOperarIngresoListNew;
            ingreso.setOperarIngresoList(operarIngresoListNew);
            List<OperarIngreso> attachedOperarIngresoList1New = new ArrayList<OperarIngreso>();
            for (OperarIngreso operarIngresoList1NewOperarIngresoToAttach : operarIngresoList1New) {
                operarIngresoList1NewOperarIngresoToAttach = em.getReference(operarIngresoList1NewOperarIngresoToAttach.getClass(), operarIngresoList1NewOperarIngresoToAttach.getIdOperarIngreso());
                attachedOperarIngresoList1New.add(operarIngresoList1NewOperarIngresoToAttach);
            }
            operarIngresoList1New = attachedOperarIngresoList1New;
            ingreso.setOperarIngresoList1(operarIngresoList1New);
            ingreso = em.merge(ingreso);
            if (idConsumidorOld != null && !idConsumidorOld.equals(idConsumidorNew)) {
                idConsumidorOld.getIngresoList().remove(ingreso);
                idConsumidorOld = em.merge(idConsumidorOld);
            }
            if (idConsumidorNew != null && !idConsumidorNew.equals(idConsumidorOld)) {
                idConsumidorNew.getIngresoList().add(ingreso);
                idConsumidorNew = em.merge(idConsumidorNew);
            }
            for (Reporte reporteListOldReporte : reporteListOld) {
                if (!reporteListNew.contains(reporteListOldReporte)) {
                    reporteListOldReporte.getIngresoList().remove(ingreso);
                    reporteListOldReporte = em.merge(reporteListOldReporte);
                }
            }
            for (Reporte reporteListNewReporte : reporteListNew) {
                if (!reporteListOld.contains(reporteListNewReporte)) {
                    reporteListNewReporte.getIngresoList().add(ingreso);
                    reporteListNewReporte = em.merge(reporteListNewReporte);
                }
            }
            for (Categoria categoriaListOldCategoria : categoriaListOld) {
                if (!categoriaListNew.contains(categoriaListOldCategoria)) {
                    categoriaListOldCategoria.getIngresoList().remove(ingreso);
                    categoriaListOldCategoria = em.merge(categoriaListOldCategoria);
                }
            }
            for (Categoria categoriaListNewCategoria : categoriaListNew) {
                if (!categoriaListOld.contains(categoriaListNewCategoria)) {
                    categoriaListNewCategoria.getIngresoList().add(ingreso);
                    categoriaListNewCategoria = em.merge(categoriaListNewCategoria);
                }
            }
            for (OperarConsumoIngreso operarConsumoIngresoListNewOperarConsumoIngreso : operarConsumoIngresoListNew) {
                if (!operarConsumoIngresoListOld.contains(operarConsumoIngresoListNewOperarConsumoIngreso)) {
                    Ingreso oldIdIngresoOfOperarConsumoIngresoListNewOperarConsumoIngreso = operarConsumoIngresoListNewOperarConsumoIngreso.getIdIngreso();
                    operarConsumoIngresoListNewOperarConsumoIngreso.setIdIngreso(ingreso);
                    operarConsumoIngresoListNewOperarConsumoIngreso = em.merge(operarConsumoIngresoListNewOperarConsumoIngreso);
                    if (oldIdIngresoOfOperarConsumoIngresoListNewOperarConsumoIngreso != null && !oldIdIngresoOfOperarConsumoIngresoListNewOperarConsumoIngreso.equals(ingreso)) {
                        oldIdIngresoOfOperarConsumoIngresoListNewOperarConsumoIngreso.getOperarConsumoIngresoList().remove(operarConsumoIngresoListNewOperarConsumoIngreso);
                        oldIdIngresoOfOperarConsumoIngresoListNewOperarConsumoIngreso = em.merge(oldIdIngresoOfOperarConsumoIngresoListNewOperarConsumoIngreso);
                    }
                }
            }
            for (TransferenciaIngreso transferenciaIngresoListNewTransferenciaIngreso : transferenciaIngresoListNew) {
                if (!transferenciaIngresoListOld.contains(transferenciaIngresoListNewTransferenciaIngreso)) {
                    Ingreso oldIdIngresoOfTransferenciaIngresoListNewTransferenciaIngreso = transferenciaIngresoListNewTransferenciaIngreso.getIdIngreso();
                    transferenciaIngresoListNewTransferenciaIngreso.setIdIngreso(ingreso);
                    transferenciaIngresoListNewTransferenciaIngreso = em.merge(transferenciaIngresoListNewTransferenciaIngreso);
                    if (oldIdIngresoOfTransferenciaIngresoListNewTransferenciaIngreso != null && !oldIdIngresoOfTransferenciaIngresoListNewTransferenciaIngreso.equals(ingreso)) {
                        oldIdIngresoOfTransferenciaIngresoListNewTransferenciaIngreso.getTransferenciaIngresoList().remove(transferenciaIngresoListNewTransferenciaIngreso);
                        oldIdIngresoOfTransferenciaIngresoListNewTransferenciaIngreso = em.merge(oldIdIngresoOfTransferenciaIngresoListNewTransferenciaIngreso);
                    }
                }
            }
            for (OperarIngreso operarIngresoListOldOperarIngreso : operarIngresoListOld) {
                if (!operarIngresoListNew.contains(operarIngresoListOldOperarIngreso)) {
                    operarIngresoListOldOperarIngreso.setIdIngresoOrigen(null);
                    operarIngresoListOldOperarIngreso = em.merge(operarIngresoListOldOperarIngreso);
                }
            }
            for (OperarIngreso operarIngresoListNewOperarIngreso : operarIngresoListNew) {
                if (!operarIngresoListOld.contains(operarIngresoListNewOperarIngreso)) {
                    Ingreso oldIdIngresoOrigenOfOperarIngresoListNewOperarIngreso = operarIngresoListNewOperarIngreso.getIdIngresoOrigen();
                    operarIngresoListNewOperarIngreso.setIdIngresoOrigen(ingreso);
                    operarIngresoListNewOperarIngreso = em.merge(operarIngresoListNewOperarIngreso);
                    if (oldIdIngresoOrigenOfOperarIngresoListNewOperarIngreso != null && !oldIdIngresoOrigenOfOperarIngresoListNewOperarIngreso.equals(ingreso)) {
                        oldIdIngresoOrigenOfOperarIngresoListNewOperarIngreso.getOperarIngresoList().remove(operarIngresoListNewOperarIngreso);
                        oldIdIngresoOrigenOfOperarIngresoListNewOperarIngreso = em.merge(oldIdIngresoOrigenOfOperarIngresoListNewOperarIngreso);
                    }
                }
            }
            for (OperarIngreso operarIngresoList1NewOperarIngreso : operarIngresoList1New) {
                if (!operarIngresoList1Old.contains(operarIngresoList1NewOperarIngreso)) {
                    Ingreso oldIdIngresoDestinoOfOperarIngresoList1NewOperarIngreso = operarIngresoList1NewOperarIngreso.getIdIngresoDestino();
                    operarIngresoList1NewOperarIngreso.setIdIngresoDestino(ingreso);
                    operarIngresoList1NewOperarIngreso = em.merge(operarIngresoList1NewOperarIngreso);
                    if (oldIdIngresoDestinoOfOperarIngresoList1NewOperarIngreso != null && !oldIdIngresoDestinoOfOperarIngresoList1NewOperarIngreso.equals(ingreso)) {
                        oldIdIngresoDestinoOfOperarIngresoList1NewOperarIngreso.getOperarIngresoList1().remove(operarIngresoList1NewOperarIngreso);
                        oldIdIngresoDestinoOfOperarIngresoList1NewOperarIngreso = em.merge(oldIdIngresoDestinoOfOperarIngresoList1NewOperarIngreso);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = ingreso.getIdIngreso();
                if (findIngreso(id) == null) {
                    throw new NonexistentEntityException("The ingreso with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Ingreso ingreso;
            try {
                ingreso = em.getReference(Ingreso.class, id);
                ingreso.getIdIngreso();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The ingreso with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<OperarConsumoIngreso> operarConsumoIngresoListOrphanCheck = ingreso.getOperarConsumoIngresoList();
            for (OperarConsumoIngreso operarConsumoIngresoListOrphanCheckOperarConsumoIngreso : operarConsumoIngresoListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Ingreso (" + ingreso + ") cannot be destroyed since the OperarConsumoIngreso " + operarConsumoIngresoListOrphanCheckOperarConsumoIngreso + " in its operarConsumoIngresoList field has a non-nullable idIngreso field.");
            }
            List<TransferenciaIngreso> transferenciaIngresoListOrphanCheck = ingreso.getTransferenciaIngresoList();
            for (TransferenciaIngreso transferenciaIngresoListOrphanCheckTransferenciaIngreso : transferenciaIngresoListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Ingreso (" + ingreso + ") cannot be destroyed since the TransferenciaIngreso " + transferenciaIngresoListOrphanCheckTransferenciaIngreso + " in its transferenciaIngresoList field has a non-nullable idIngreso field.");
            }
            List<OperarIngreso> operarIngresoList1OrphanCheck = ingreso.getOperarIngresoList1();
            for (OperarIngreso operarIngresoList1OrphanCheckOperarIngreso : operarIngresoList1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Ingreso (" + ingreso + ") cannot be destroyed since the OperarIngreso " + operarIngresoList1OrphanCheckOperarIngreso + " in its operarIngresoList1 field has a non-nullable idIngresoDestino field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Consumidor idConsumidor = ingreso.getIdConsumidor();
            if (idConsumidor != null) {
                idConsumidor.getIngresoList().remove(ingreso);
                idConsumidor = em.merge(idConsumidor);
            }
            List<Reporte> reporteList = ingreso.getReporteList();
            for (Reporte reporteListReporte : reporteList) {
                reporteListReporte.getIngresoList().remove(ingreso);
                reporteListReporte = em.merge(reporteListReporte);
            }
            List<Categoria> categoriaList = ingreso.getCategoriaList();
            for (Categoria categoriaListCategoria : categoriaList) {
                categoriaListCategoria.getIngresoList().remove(ingreso);
                categoriaListCategoria = em.merge(categoriaListCategoria);
            }
            List<OperarIngreso> operarIngresoList = ingreso.getOperarIngresoList();
            for (OperarIngreso operarIngresoListOperarIngreso : operarIngresoList) {
                operarIngresoListOperarIngreso.setIdIngresoOrigen(null);
                operarIngresoListOperarIngreso = em.merge(operarIngresoListOperarIngreso);
            }
            em.remove(ingreso);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Ingreso> findIngresoEntities() {
        return findIngresoEntities(true, -1, -1);
    }

    public List<Ingreso> findIngresoEntities(int maxResults, int firstResult) {
        return findIngresoEntities(false, maxResults, firstResult);
    }

    private List<Ingreso> findIngresoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Ingreso.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Ingreso findIngreso(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Ingreso.class, id);
        } finally {
            em.close();
        }
    }

    public int getIngresoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Ingreso> rt = cq.from(Ingreso.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
